#include <aknviewappui.h>
#include "CSingaporeBusGenericView.h"
#include "CSingaporeBusGenericView.hrh"
#include "FunTrace.h"
#include "SingaporeBusDocument.h"
#include "SingaporeBusAppUi.h"
#include <SingaporeBus.hrh>
#include <Binder.h>
#include "CSingaporeBusRouteView.h"
#include <aknsbasicbackgroundcontrolcontext.h>
#include "CharUtils.h"
#include "CSingaporeBusFavoritesView.h"
const TInt DndTimedOut = -5120;

template<class T>
inline T Square(const T& aT)
	{
	return aT*aT;
	}

static TInt serviceOrder(const TDesC8& aDesC)
	{
	TInt order = 0;
	for(int i = 0 ; i != aDesC.Length() ; ++i)
		{
		if(aDesC[i] >= '0' && aDesC[i] <= '9')
			order = 10*order + (aDesC[i] - '0');
		}
	return order;
	}


CArrivalTimeItem::CArrivalTimeItem() : iNextBus(NULL), iSubsequentBus(NULL), iFormat(NULL), iArrivalTimeQuery(NULL), iCallback(NULL)
	{
	
	}
CArrivalTimeItem::~CArrivalTimeItem()
	{
	delete iArrivalTimeQuery; // must be first!
	delete iNextBus;
	delete iSubsequentBus;
	delete iFormat;
	}


void CreateArrivalTimeQuery(CArrivalTimeItem* aArrivalTime)
{
CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	if(aArrivalTime->iService->iOperator == _L8("SBST"))
		aArrivalTime->iArrivalTimeQuery = new (ELeave) CIrisQuery(
				doc->iIrisSession.Get(),
				aArrivalTime->iService->iName,
				aArrivalTime->iStation->iStationNumber,
				(aArrivalTime->iStation->iStationType == TStation::ESGInterchange),
				scalp::MakeFunctionL( scalp::Bind(&CSingaporeBusGenericHandler::IrisCallback, aArrivalTime, scalp::Free1, scalp::Free2, scalp::Free3, scalp::Free4, scalp::Free5, scalp::Free6, scalp::Free7) )
				);
	else if(aArrivalTime->iService->iOperator == _L8("SMRT"))
		aArrivalTime->iArrivalTimeQuery = new (ELeave) CSmrtQuery(
				doc->iSmrtSession.Get(),
				aArrivalTime->iService->iName,
				aArrivalTime->iStation->iStationNumber,
				scalp::MakeFunctionL( scalp::Bind(&CSingaporeBusGenericHandler::SmrtCallback, aArrivalTime, scalp::Free1, scalp::Free2, scalp::Free3, scalp::Free4, scalp::Free5, scalp::Free6, scalp::Free7) )
				);
	else if(aArrivalTime->iService->iOperator == _L8("TFL"))
		aArrivalTime->iArrivalTimeQuery = new (ELeave) CTflQuery(
				doc->iTflSession.Get(),
				aArrivalTime->iService->iName,
				aArrivalTime->iStation->iStationNumber,
				scalp::MakeFunctionL( scalp::Bind(&CSingaporeBusGenericHandler::TflCallback, aArrivalTime, scalp::Free1, scalp::Free2, scalp::Free3, scalp::Free4, scalp::Free5, scalp::Free6, scalp::Free7) )
				);
}



TBool CSingaporeBusGenericHandler::FilterItem(const TStation* aItem, const TDesC8& aSearchText)
	{
	return
		(
		aSearchText.Length() == 0 ||
		aItem->iStationNumber.FindF(aSearchText) != KErrNotFound ||
		aItem->iAddress.FindF(aSearchText) != KErrNotFound ||
		aItem->iAddress2.FindF(aSearchText) != KErrNotFound
		);
	}
TBool CSingaporeBusGenericHandler::FilterItem(const TService* aItem, const TDesC8& aSearchText)
	{
	return
		(
		aSearchText.Length() == 0 ||
		aItem->iName.FindF(aSearchText) != KErrNotFound ||
		aItem->iFrom.FindF(aSearchText) != KErrNotFound ||
		aItem->iTo.FindF(aSearchText) != KErrNotFound
		);
	}
TBool CSingaporeBusGenericHandler::FilterItem(const CArrivalTimeItem* aItem, const TDesC8& aSearchText)
	{
	return
		(
		aSearchText.Length() == 0 ||
		aItem->iService->iName.FindF(aSearchText) != KErrNotFound ||
		aItem->iService->iFrom.FindF(aSearchText) != KErrNotFound ||
		aItem->iService->iTo.FindF(aSearchText) != KErrNotFound ||
		aItem->iStation->iStationNumber.FindF(aSearchText) != KErrNotFound ||
		aItem->iStation->iAddress.FindF(aSearchText) != KErrNotFound ||
		aItem->iStation->iAddress2.FindF(aSearchText) != KErrNotFound
		);
	}


TReal32 CSingaporeBusGenericHandler::OrderItem(const TStation* aItem)
	{
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	TReal32 distance = 0;
	if(doc->iPositioningStatus == CSingaporeBusDocument::PS_FOUND)
		distance = (Square(doc->iPosition.Longitude() - aItem->iCoord.Longitude()) +
		Square(doc->iPosition.Latitude() - aItem->iCoord.Latitude()))*1000000000.0
		;
	return distance;
	}
TReal32 CSingaporeBusGenericHandler::OrderItem(const TService* aItem)
	{
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	TBuf8<15> upperServiceKey = aItem->iServiceKey;
	upperServiceKey.UpperCase();
	const TStation** pStation = doc->iServicesStationMap->Find(upperServiceKey);
	if(pStation)
		{
		const TStation* station = *pStation;
		TReal32 order = 
			(Square(doc->iPosition.Longitude() - station->iCoord.Longitude()) +
			Square(doc->iPosition.Latitude() - station->iCoord.Latitude()))*1000000000.0
			+serviceOrder(aItem->iName);
		return order;
		}
	else
		return KMaxTReal32;
	}

TReal32 CSingaporeBusGenericHandler::OrderItem(const CArrivalTimeItem* aItem)
	{
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	TReal32 distance = 0;
	if(doc->iPositioningStatus == CSingaporeBusDocument::PS_FOUND)
		distance = (Square(doc->iPosition.Longitude() - aItem->iStation->iCoord.Longitude()) +
		Square(doc->iPosition.Latitude() - aItem->iStation->iCoord.Latitude()))*1000000000.0
		+serviceOrder(aItem->iService->iName);
		;
	return distance;
	}


TBool CSingaporeBusGenericHandler::ListBoxObserver(const TStation* aItem, CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	switch(aEventType)
		{
		case MEikListBoxObserver::EEventEnterKeyPressed:
		case MEikListBoxObserver::EEventItemClicked: // single tap
			{
			TBuf8<128> buf;
			StoreCustomMessageParamL(buf, _L8("id"), _L8("%S"), &aItem->iStationNumber);
			iAvkonViewAppUi->ActivateLocalViewL(TUid::Uid( ESingaporeBusServicesArrivalTimeViewId ), TUid::Uid(0), buf);
			return ETrue;
			}
		}
	return EFalse;
	}
TBool CSingaporeBusGenericHandler::ListBoxObserver(const TService* aItem, CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	switch(aEventType)
		{
		case MEikListBoxObserver::EEventEnterKeyPressed:
		case MEikListBoxObserver::EEventItemClicked: // single tap
			{
			CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
			TBuf8<128> buf;
			TBuf8<15> upperServiceKey = aItem->iServiceKey;
			upperServiceKey.UpperCase();
			const TStation** pStation = doc->iServicesStationMap->Find(upperServiceKey);
			if(pStation)
				StoreCustomMessageParamL(buf, _L8("station"), _L8("%S"), &(*pStation)->iStationNumber);
			StoreCustomMessageParamL(buf, _L8("id"), _L8("%S"), &aItem->iServiceKey);
			iAvkonViewAppUi->ActivateLocalViewL(TUid::Uid( ESingaporeBusServiceRouteViewId ), TUid::Uid(0), buf);
			return ETrue;
			}
		}
	return EFalse;
	}
TBool CSingaporeBusGenericHandler::ListBoxObserver(const CArrivalTimeItem* aItem, CEikListBox* aListBox, MEikListBoxObserver::TListBoxEvent aEventType)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	switch(aEventType)
		{
		case MEikListBoxObserver::EEventEnterKeyPressed:
		case MEikListBoxObserver::EEventItemClicked: // single tap
			{
			if(!aItem->iArrivalTimeQuery) CreateArrivalTimeQuery(const_cast<CArrivalTimeItem*>(aItem));
			aItem->iArrivalTimeQuery->SubmitL();
			return ETrue;
			}
		}
	return EFalse;
	}


static TInt SelectFavoritePaneL()
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	// Make listitems. and PUSH it
	LCleanedupPtr<CAknSinglePopupMenuStyleListBox> list ( new(ELeave) CAknSinglePopupMenuStyleListBox );
	LCleanedupPtr<CAknPopupList> popupList ( CAknPopupList::NewL(list.Get(), R_AVKON_SOFTKEYS_OK_CANCEL, AknPopupLayouts::EMenuWindow) );
	LCleanedupPtr<CDesCArrayFlat> items ( new (ELeave) CDesCArrayFlat(5) );
	list->ConstructL(popupList.Get(), CEikListBox::ELeftDownInViewRect | EAknListBoxMarkableList);
	list->CreateScrollBarFrameL(ETrue);
	list->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
	for( TInt i = 0 ; i < doc->iFavoritesTitles.Count() ; ++i )
		items->AppendL(*doc->iFavoritesTitles[i]);
	CTextListBoxModel* model = list->Model();
	model->SetItemTextArray(items.Unmanage());
	model->SetOwnershipType(ELbmOwnsItemArray);
	popupList->SetTitleL(_L("Panes"));
	list->SetListBoxObserver(popupList.Get());
	list->ItemDrawer()->FormattedCellData()->EnableMarqueeL(ETrue);
	TInt popupOk = popupList.Unmanage()->ExecuteLD();
	TInt favoritePanel = KErrNotFound;
	if (popupOk)
		favoritePanel = list->CurrentItemIndex();
	return favoritePanel;
	}


TBool CSingaporeBusGenericHandler::HandleCommandL(const TStation* aItem, TInt aCommand )
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();

	switch ( aCommand )
		{
		case ESingaporeBusShowMap:
			{
			TBuf8<256> buf;
			StoreCustomMessageParamL(buf, _L8("station"), _L8("%S"), &aItem->iStationNumber);
			iAvkonViewAppUi->ActivateLocalViewL(TUid::Uid(EPublicTransportMapContainerViewId), TUid::Uid(0), buf);
			return ETrue;
			}
		case ESingaporeBusMarkOrigin:
			{
			doc->SetOriginL(aItem);
			return ETrue;
			}
		case ESingaporeBusMarkDestination:
			{
			doc->SetDestinationL(aItem);
			return ETrue;
			}
		case ESingaporeBusRefreshPosition:
			{
			doc->RefreshPosition();
			return ETrue;
			}
		case ESingaporeBusMarkFavorite:
			{
			CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
			TInt favorite = -1;
			TVwsViewId viewId;
			CSingaporeBusFavoritesView* favoriteView = NULL;
			if(iAvkonViewAppUi->GetActiveViewId(viewId) == KErrNone &&
					(viewId.iViewUid.iUid == ESingaporeBusFavoritesViewId || viewId.iViewUid.iUid == ESingaporeBusFavoritesViewId2) )
				{
				favoriteView = (CSingaporeBusFavoritesView*)iAvkonViewAppUi->View( viewId.iViewUid );
				favorite = favoriteView->iCurrentFavorite;
				}
			else if(doc->iFavorites.Count() > 1)
				favorite = SelectFavoritePaneL();
			else if(doc->iFavorites.Count() > 0)
				favorite = 0;
			if(favorite >= 0)
				{
				TBuf8<128> favoriteStr;
				doc->StoreFavoriteL(favoriteStr, aItem);
				doc->ToggleFavoriteL(favoriteStr, favorite);
				if(favoriteView)
					favoriteView->InitializeList();
				}
			return ETrue;
			}
		}
	return EFalse;
	}
TBool CSingaporeBusGenericHandler::HandleCommandL(const TService* aItem, const TStation* aStation, TInt aCommand )
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	switch ( aCommand )
		{
		case ESingaporeBusShowMap:
			{
			TBuf8<1024> buf;
			StoreCustomMessageParamL(buf, _L8("service"), _L8("%S"), &aItem->iServiceKey);
			StoreCustomMessageParamL(buf, _L8("path"), _L8("%S"), &aItem->iServiceKey);
			StoreCustomMessageParamL(buf, _L8("curpath"), _L8("0"));
			if(aStation)
				StoreCustomMessageParamL(buf, _L8("station"), _L8("%S"), &aStation->iStationNumber);
			iAvkonViewAppUi->ActivateLocalViewL(TUid::Uid(EPublicTransportMapContainerViewId), TUid::Uid(0), buf);
			return ETrue;
			}
		case ESingaporeBusRefreshPosition:
			{
			doc->RefreshPosition();
			return ETrue;
			}
		case ESingaporeBusShowServiceRoute:
			{
			TBuf8<1024> buf;
			StoreCustomMessageParamL(buf, _L8("id"), _L8("%S"), &aItem->iServiceKey);
			if(aStation)
				StoreCustomMessageParamL(buf, _L8("station"), _L8("%S"), &aStation->iStationNumber);
			iAvkonViewAppUi->ActivateLocalViewL(TUid::Uid(ESingaporeBusServiceRouteViewId), TUid::Uid(0), buf);
			return ETrue;
			}
		case ESingaporeBusMarkFavorite:
			{
			CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
			TInt favorite = -1;
			TVwsViewId viewId;
			CSingaporeBusFavoritesView* favoriteView = NULL;
			if(iAvkonViewAppUi->GetActiveViewId(viewId) == KErrNone &&
					(viewId.iViewUid.iUid == ESingaporeBusFavoritesViewId || viewId.iViewUid.iUid == ESingaporeBusFavoritesViewId2) )
				{
				favoriteView = (CSingaporeBusFavoritesView*)iAvkonViewAppUi->View( viewId.iViewUid );
				favorite = favoriteView->iCurrentFavorite;
				}
			else if(doc->iFavorites.Count() > 1)
				favorite = SelectFavoritePaneL();		
			else if(doc->iFavorites.Count() > 0)
				favorite = 0;		
			if(favorite >= 0)
				{
				TBuf8<128> favoriteStr;
				doc->StoreFavoriteL(favoriteStr, aItem);
				doc->ToggleFavoriteL(favoriteStr, favorite);
				if(favoriteView)
					favoriteView->InitializeList();
				}
			return ETrue;
			}
		}
	return EFalse;
	}
TBool CSingaporeBusGenericHandler::HandleCommandL(const CArrivalTimeItem* aItem, TInt aCommand )
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();

	switch ( aCommand )
		{
		case ESingaporeBusShowMap:
			{
			TBuf8<256> buf;
			StoreCustomMessageParamL(buf, _L8("station"), _L8("%S"), &aItem->iStation->iStationNumber);
			StoreCustomMessageParamL(buf, _L8("service"), _L8("%S"), &aItem->iService->iServiceKey);
			StoreCustomMessageParamL(buf, _L8("path"), _L8("%S"), &aItem->iService->iServiceKey);
			StoreCustomMessageParamL(buf, _L8("curpath"), _L8("0"));
			iAvkonViewAppUi->ActivateLocalViewL(TUid::Uid(EPublicTransportMapContainerViewId), TUid::Uid(0), buf);
			return ETrue;
			}
		case ESingaporeBusShowServiceRoute:
			{
			TBuf8<128> buf;
			StoreCustomMessageParamL(buf, _L8("station"), _L8("%S"), &aItem->iStation->iStationNumber);
			StoreCustomMessageParamL(buf, _L8("id"), _L8("%S"), &aItem->iService->iServiceKey);
			iAvkonViewAppUi->ActivateLocalViewL(TUid::Uid( ESingaporeBusServiceRouteViewId ), TUid::Uid(0), buf);
			return ETrue;
			}
		case ESingaporeBusMarkOrigin:
			{
			doc->SetOriginL(aItem->iStation);
			return ETrue;
			}
		case ESingaporeBusMarkDestination:
			{
			doc->SetDestinationL(aItem->iStation);
			return ETrue;
			}
		case ESingaporeBusRefreshPosition:
			{
			doc->RefreshPosition();
			return ETrue;
			}
		case ESingaporeBusRefreshAll:
			{
			if(!aItem->iArrivalTimeQuery) CreateArrivalTimeQuery(const_cast<CArrivalTimeItem*>(aItem));
			aItem->iArrivalTimeQuery->SubmitL();
			return ETrue;
			}
		case ESingaporeBusMarkFavorite:
			{
			CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
			TInt favorite = -1;
			TVwsViewId viewId;
			CSingaporeBusFavoritesView* favoriteView = NULL;
			if(iAvkonViewAppUi->GetActiveViewId(viewId) == KErrNone &&
					(viewId.iViewUid.iUid == ESingaporeBusFavoritesViewId || viewId.iViewUid.iUid == ESingaporeBusFavoritesViewId2) )
				{
				favoriteView = (CSingaporeBusFavoritesView*)iAvkonViewAppUi->View( viewId.iViewUid );
				favorite = favoriteView->iCurrentFavorite;
				}
			else if(doc->iFavorites.Count() > 1)
				favorite = SelectFavoritePaneL();
			else if(doc->iFavorites.Count() > 0)
				favorite = 0;
			if(favorite >= 0)
				{
				TBuf8<128> favoriteStr;
				doc->StoreFavoriteL(favoriteStr, aItem->iStation, aItem->iService);
				doc->ToggleFavoriteL(favoriteStr, favorite);
				if(favoriteView)
					favoriteView->InitializeList();
				}
			return ETrue;
			}
		}
	
	return EFalse;
	}




CArrivalTimeItem* CSingaporeBusGenericHandler::NewArrivalTimeItemL(const TStation* aStation, const TService *aService, CArrivalTimeItemCallback* aCallback)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	LCleanedupPtr<CArrivalTimeItem> ret ( new (ELeave) CArrivalTimeItem );
	CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
	ret->iService = aService;
	ret->iStation = aStation;
	ret->iCallback = aCallback;
	ret->iRequestStatus = CArrivalTimeQuery::ERequestWait;
	return ret.Unmanage();
	}


void CSingaporeBusGenericHandler::IrisCallback(CArrivalTimeItem* aItem, CIrisQuery*, const TDesC8& aStationNumber, const TDesC8& aServiceName, CArrivalTimeQuery::TServiceRequestStatus aServiceRequestStatus, const TDesC& aFormat, const TDesC8& aNextBus, const TDesC8& aSubsequentBus)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	if(aItem->iService->iName.CompareF(aServiceName) == 0)
		{
		aItem->iRequestStatus = aServiceRequestStatus;
		delete aItem->iNextBus;
		aItem->iNextBus = narrowToWideL(aNextBus);
		delete aItem->iSubsequentBus;
		aItem->iSubsequentBus = narrowToWideL(aSubsequentBus);
		if(aItem->iCallback)
			aItem->iCallback->ArrivalTimeItemCallback(aItem);
		if(aItem->iRequestStatus == CArrivalTimeQuery::ERequestError)
			{
			CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
			TLex8 lex(aNextBus);
			TInt error; lex.Val(error);
			if(error == KErrNotReady || error == KErrConnectionTerminated || error == DndTimedOut) // not connected or connection lost
				doc->ConnectL();
			}
		
		}
	
	}

void CSingaporeBusGenericHandler::SmrtCallback(CArrivalTimeItem* aItem, CSmrtQuery*, const TDesC8& aStationNumber, const TDesC8& aServiceName, CArrivalTimeQuery::TServiceRequestStatus aServiceRequestStatus, const TDesC& aFormat, const TDesC8& aNextBus, const TDesC8& aSubsequentBus)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	if(aItem->iService->iName.CompareF(aServiceName) == 0)
		{
		aItem->iRequestStatus = aServiceRequestStatus;
		delete aItem->iNextBus;
		aItem->iNextBus = narrowToWideL(aNextBus);
		delete aItem->iSubsequentBus;
		aItem->iSubsequentBus = narrowToWideL(aSubsequentBus);
		if(aItem->iCallback)
			aItem->iCallback->ArrivalTimeItemCallback(aItem);
		if(aItem->iRequestStatus == CArrivalTimeQuery::ERequestError)
			{
			CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
			TLex8 lex(aNextBus);
			TInt error; lex.Val(error);
			if(error == KErrNotReady || error == KErrConnectionTerminated || error == DndTimedOut) // not connected or connection lost
				doc->ConnectL();
			}
		
		}
	
	}


void CSingaporeBusGenericHandler::TflCallback(CArrivalTimeItem* aItem, CTflQuery*, const TDesC8& aStationNumber, const TDesC8& aServiceName, CArrivalTimeQuery::TServiceRequestStatus aServiceRequestStatus, const TDesC& aFormat, const TDesC8& aNextBus, const TDesC8& aSubsequentBus)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	if(aItem->iService->iName.CompareF(aServiceName) == 0)
		{
		aItem->iRequestStatus = aServiceRequestStatus;
		delete aItem->iNextBus;
		aItem->iNextBus = narrowToWideL(aNextBus);
		delete aItem->iSubsequentBus;
		aItem->iSubsequentBus = narrowToWideL(aSubsequentBus);
		if(aItem->iCallback)
			aItem->iCallback->ArrivalTimeItemCallback(aItem);
		if(aItem->iRequestStatus == CArrivalTimeQuery::ERequestError)
			{
			CSingaporeBusDocument* doc = (CSingaporeBusDocument*)CEikonEnv::Static()->EikAppUi()->Document();
			TLex8 lex(aNextBus);
			TInt error; lex.Val(error);
			if(error == KErrNotReady || error == KErrConnectionTerminated || error == DndTimedOut) // not connected or connection lost
				doc->ConnectL();
			}
		
		}
	
	}





void StoreCustomMessageParamL(TDes8& aCustomMessage, const TDesC8& aField, TRefByValue<const TDesC8> aFormat, ...)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	aCustomMessage.Append(aField);
	aCustomMessage.Append('=');
    VA_LIST list;
    VA_START(list, aFormat);
    aCustomMessage.AppendFormatList(aFormat, list);
    VA_END(list);
	aCustomMessage.Append('\n');
	}


TBool ParseCustomMessageParamL(TPtrC8& aField, TPtrC8& aValue, TPtrC8& aRemainder)
	{
	TFunTrace funTrace(__PRETTY_FUNCTION__);
	const TInt sep = aRemainder.Locate('=');
	if(sep == KErrNotFound) return EFalse;
	const TInt end = aRemainder.Locate('\n');
	if(end == KErrNotFound) return EFalse;
	aField.Set(aRemainder.Mid(0, sep));
	aValue.Set(aRemainder.Mid(sep+1, end-1-sep));
	aRemainder.Set( aRemainder.Mid(end+1) );
	return ETrue;
	}

